#!/usr/bin/env python3

import re
import os

def fix_regex_patterns(file_path):
    """
    Fix the regex pattern group name mismatch in CppCodeParser.py.
    
    This script addresses the issue where regex patterns use 'n' as the group name
    but the code tries to access it as 'name'.
    
    Args:
        file_path: Path to the CppCodeParser.py file
    """
    print(f"Fixing regex patterns in {file_path}")
    
    # Read the file content
    with open(file_path, 'r') as f:
        content = f.read()
    
    # Replace all instances of (?P<n> with (?P<name> in regex patterns
    modified_content = re.sub(r'\(\?P<n>', r'(?P<name>', content)
    
    # Write back the modified content
    with open(file_path, 'w') as f:
        f.write(modified_content)
    
    print(f"Fixed regex patterns in {file_path}")

if __name__ == "__main__":
    # Path to the file
    file_path = 'cpp_flow_map/Tools/CppCodeParser.py'
    
    # Check if the file exists
    if not os.path.exists(file_path):
        print(f"Error: File {file_path} does not exist")
        exit(1)
    
    # Create a backup
    backup_path = f"{file_path}.bak"
    print(f"Creating backup at {backup_path}")
    with open(file_path, 'r') as src, open(backup_path, 'w') as dst:
        dst.write(src.read())
    
    # Fix the regex patterns
    fix_regex_patterns(file_path) 